package edu.ncsu.csc.ip.routing.ls;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;

/**
 * Class <code>RunTask1</code>
 * 
 * Run Task1 In P3 description 
 * 
 * @author Moin Ayazifar <mayazif@ncsu.edu>
 * @author Steven Elliott <sdelliot@ncsu.edu>
 * 
 */


public class RunTask1 {

	public static Comparator<Node> NodeNameComparator = new Comparator<Node>() {
		public int compare(Node node1, Node node2) {
			// ascending order
			return Integer.valueOf(node1.getName()).compareTo(Integer.valueOf(node2.getName()));
		}
	};

	public static void main(String[] args) throws IOException {

		Map<Node, Long> timeMap = new HashMap<Node, Long>();
		long time = System.nanoTime();
		LinkState linkState = new LinkState();
		linkState.loadGraph(args[0]);

		for (Node source : linkState.nodesMap.values()) {
			time = System.nanoTime();
			linkState.initialize();
			linkState.populateLeastCostPaths(source);
			time = System.nanoTime() - time;
			timeMap.put(source, new Long(time / 1000));
		}

		
		
		Node[] nodes = timeMap.keySet().toArray(new Node[timeMap.size()]);
		Arrays.sort(nodes, NodeNameComparator);
		
		try {
			File file = new File("./results/Task1_results.csv");
			PrintWriter printWrite = new PrintWriter(
					new FileWriter(file, false));
			printWrite.println("Source Node , Time, Cumulative Time");
			long cumulativeTime = 0;
			for (Node node : nodes) {
				cumulativeTime += timeMap.get(node).longValue();
				printWrite.println(node + "," + timeMap.get(node).longValue()
						+ "," + cumulativeTime);
			}
			printWrite.close();
		} catch (Exception e) {
           e.printStackTrace();
		}
	}

	public static void main_total_time(String[] args) throws IOException {
		
		LinkState linkState = new LinkState();
		linkState.loadGraph(args[0]);
		long time = System.nanoTime();
		for (Node source : linkState.nodesMap.values()) {
			linkState.initialize();
			linkState.populateLeastCostPaths(source);
		}
		time = System.nanoTime() - time;
		double timeInMicro = time / 1000;
		System.out
				.println("Time to calculate shortest path from all nodes to all nodes: ="
						+ timeInMicro + " microseconds.");
	}

}
